home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / SATAN11.ZIP / SRC / PORT_SCA / FIND_ADD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-04  |  1.2 KB  |  52 lines

  1.  /*
  2.   * find_addr, find_port - map internet hosts and services to internal form
  3.   * 
  4.   * Author: Wietse Venema.
  5.   */
  6.  
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <netinet/in.h>
  10. #include <arpa/inet.h>
  11. #include <netdb.h>
  12.  
  13. #include "lib.h"
  14.  
  15. /* find_addr - translate numerical or symbolic host name */
  16.  
  17. struct in_addr find_addr(host)
  18. char   *host;
  19. {
  20.     struct in_addr addr;
  21.     struct hostent *hp;
  22.  
  23.     addr.s_addr = inet_addr(host);
  24.     if ((addr.s_addr == -1) || (addr.s_addr == 0)) {
  25.     if ((hp = gethostbyname(host)) == 0)
  26.         error("%s: host not found", host);
  27.     if (hp->h_addrtype != AF_INET)
  28.         error("unexpected address family: %d", hp->h_addrtype);
  29.     memcpy((char *) &addr, hp->h_addr, hp->h_length);
  30.     }
  31.     return (addr);
  32. }
  33.  
  34. /* find_port - translate numerical or symbolic service name */
  35.  
  36. int     find_port(service, protocol)
  37. char   *service;
  38. char   *protocol;
  39. {
  40.     struct servent *sp;
  41.     int     port;
  42.  
  43.     if ((port = atoi(service)) != 0) {
  44.     return (htons(port));
  45.     } else {
  46.     if ((sp = getservbyname(service, protocol)) == 0)
  47.         error("%s/%s: unknown service", service, protocol);
  48.     return (sp->s_port);
  49.     }
  50. }
  51.  
  52.